let BaseController = require('./BaseController')
let fs = require('fs')
let path = require('path')
class Controller extends BaseController {
    constructor() {
        super();
        this.baseSrc = `d:/数据指标分类/${new Date().format('yyyyMMdd')}/待入库/11指数/中国股市指数/`;
        this.encode = 'UTF-8'
        this.params = {
            pageIndex: 1,
            pageTotal: ''
        }


        this.data = []
    };
    /**
     * 获取url
     */
    getUrl(type) {
        //http://quote.eastmoney.com/centerv2/hszs
        if (type === 1) {
            return `http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd=0000011,3990012,3990062,3990052,0003001,0000031,0000021,3991062,3990032,0000161,3995502,3990022&sty=FCOIATA&sortType=(Code)&sortRule=1&page=1&pageSize=12&js=var%20OdglnlyM={rank:[(x)],pages:(pc),total:(tot)}&token=7bc05d0d4c3c22ef9fca8c2a912d779c&jsName=quote_123&_g=0.628606915911589&_=1533635954859`
        } else if (type == 2) {
            return `http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd=C.1&sty=FCOIATA&sortType=(ChangePercent)&sortRule=-1&page=1&pageSize=5&js=var%20lKCCFifV={rank:[(x)],pages:(pc),total:(tot)}&token=7bc05d0d4c3c22ef9fca8c2a912d779c&jsName=quote_123&_g=0.628606915911589&_=1533635954860`
        } else {
            return `http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd=C.5&sty=FCOIATA&sortType=(ChangePercent)&sortRule=-1&page=1&pageSize=5&js=var%20INuuZEEY={rank:[(x)],pages:(pc),total:(tot)}&token=7bc05d0d4c3c22ef9fca8c2a912d779c&jsName=quote_123&_g=0.628606915911589&_=1533635954861`
        }
    }

    /**
     * 获取历史数据
     */
    async getHistory() {
        console.log('39中国股市指数Controller')
        let result = await this.getData(1, this.isCache)
        this.data = [...this.data, ...this.axiosDataFormat(result.toString()).rank];
        result = await this.getData(2, this.isCache)
        this.data = [...this.data, ...this.axiosDataFormat(result.toString()).rank];
        result = await this.getData(3, this.isCache)
        this.data = [...this.data, ...this.axiosDataFormat(result.toString()).rank];
        this.writeCsv(this.data.join('\n'));
    };

    /**
     * 数据格式化
     */
    axiosDataFormat(data) {
        let result = null
        try {
            result = JSON.parse(data.replace(/^var\s\w+?\s*\=/, '').trim())
        } catch (err) {
            try {
                result = eval('(' + data.replace(/^var\s\w+?\s*\=/, '').trim() + ')')
            } catch (err) {
                if (data.indexOf('502 Bad Gateway') > -1) {
                    return []
                } else {
                    console.log(err)
                }

            }
        }
        return result
    }

    /**
     * 写csv
     */
    writeCsv(arr) {
        if (!fs.existsSync(this.baseSrc)) {
            this.mkdirs(this.baseSrc)
        }
        if (arr && arr.length) {
            let content = '序号,代码,名称,最新价,涨跌额,涨跌幅,-,成交量,成交额,昨收,今开,最高,最低,-,-,-,-,-,-,-,-,-,-,-,-,-\n'
            content += arr
            fs.writeFileSync(path.join(this.baseSrc, `中国股市指数_${new Date().format('yyyyMMdd')}.csv`), content, 'UTF-8')
        }
    }

    async main(startDate = '2015-01-01', isCache = true) {
        this.isCache = isCache;
        await this.getHistory()
        // this.writeCsv(this.data);
    }
}

let controller = new Controller()
// controller.main()
module.exports = controller